<template>
  <!-- 柱状图-一个柱子-带滚动条 -->
  <div class="echartBar" ref="top2"></div>
</template>

<script>
export default {
  components: {},
  props: ['chartData'],
  data() {
    return {}
  },
  computed: {},
  watch: {
    chartData: {
      handler(val) {
        this.getEchartData();
      },
      deep: true,
    }
  },
  created() { },
  mounted() {
    this.getEchartData()
  },
  methods: {
    getEchartData() {
      let chart = this.$echarts.init(this.$refs.top2);

      const xData = this.chartData.map(item => item.name);
      const yData1 = this.chartData.map(item => item.value1);
      console.log('bardataX::',xData);
      console.log('bardataY::',yData1);

      let option = {
        grid: {
          top: "15%",
          left: "2%",
          right: "2%",
          bottom: "2%",
          containLabel: true,
        },
        legend: {
          show:false,
          textStyle: {
            fontFamily: 'Source Han Sans CN',
            fontWeight: 400,
            fontSize: 14,
            color: "#1D4A77",
          },
        },
        tooltip: {
          show: true,
          trigger: "axis",
          textStyle: {
            color: "#1D4A77",
          },
          axisPointer: {
            type: 'shadow',
          },
          backgroundColor: "rgba(255, 255, 255, 1)", //设置背景颜色
          borderColor: "transparent",
          confine: false,
          formatter: function (params) {
            var str = params[0].name;
            params.filter(function (item) {
              if (item.componentSubType == "bar") {
                str += item.seriesName + "：" + item.value + '人';
              }
            });
            return str;
          },
        },
        dataZoom: [
          {
            show: xData.length > 6,
            // 设置滚动条类型
            type: "slider",
            // 数据窗口范围的起始数值
            startValue: 0,
            // 数据窗口范围的结束数值（一页显示多少条数据）
            endValue: 6,
            // 设置滚动条宽度，相对于盒子宽度
            width: "92%",
            // 设置滚动条高度
            height: 3,
            // 设置滚动条显示位置
            left: "8%",
            // dataZoom-slider组件离容器下侧的距离
            bottom: 0,
            // 设置选中范围的填充颜色
            fillerColor: "#00F5FE",
            // 设置边框颜色
            borderColor: "rgba(19, 120, 172, 1)",
            // 设置背景颜色
            backgroundColor: "#449FED",
            // 是否显示detail，即拖拽时候显示详细数值信息
            showDetail: false,
            //是否显示数据阴影 默认auto
            showDataShadow: false,
            brushSelect: false,
            // 是否锁定选择区域（或叫做数据窗口）的大小
            zoomLoxk: true,
            // empty：当前数据窗口外的数据，被设置为空，即不会影响其他轴的数据范围
            filterMode: "empty",
            // 控制手柄的尺寸
            handleSize: 0,
            moveHandleStyle: {
              opacity: 0,
            },
          },
          {
            // 没有下面这块的话，只能拖动滚动条，
            // 鼠标滚轮在区域内不能控制外部滚动条
            type: "inside",
            // 滚轮是否触发缩放
            zoomOnMouseWheel: false,
            // 鼠标滚轮触发滚动
            moveOnMouseMove: true,
            moveOnMouseWheel: true,
            startValue: 0,
            endValue: 22,
          },
        ],
        xAxis: {
          type: "category",
          data: xData,
          splitLine: {show: false},
          axisTick: {show: false},
          splitArea: {show: false},
          axisLine: {
            lineStyle: {
              color: "rgba(176, 222, 248, 1)",
            },
          },
          axisLabel: {
            interval: 0,
            textStyle: {
              color: "#1D4A77",
              fontSize: "14px",
              align: "center",
            },
            formatter: function (value) {
              return value.length > 4 ? value.slice(0, 4) + '...' : value; // 只显示前4个字符，并添加省略号
            }
          },
        },
        yAxis: {
          type: "value",
          name: "人",
          splitNumber: yData1.every(value => value <= 1)?1:6,
          nameTextStyle: {
            color: "#1D4A77",
            fontSize: 14,
            align: "center",
            padding: [0, 30, 0, 0],
          },
          axisLabel: {
            formatter: "{value}",
            color: "rgba(95, 187, 235, 1)",
            textStyle: {
              fontSize: 14,
              color: "#1D4A77",
              lineHeight: 16,
            },
          },
          axisTick: {show: false},
          axisLine: {show: false},
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(176, 222, 248, 1)",
              type: "dashed",
            },
          },
        },
        series: [
          {
            z: 1,
            name: '',
            type: "bar",
            data: yData1,
            barWidth: 18,
            barGap: "0%",
            itemStyle: {
              normal: {
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: "rgba(53, 161, 248, 1)", },
                  { offset: 1,  color: "rgba(53, 161, 248, 0.3)", },
                ], false ),
              }
            },
          },
          // {
          //   z: 1,
          //   name: '',
          //   type: "bar",
          //   data: yData2,
          //   barWidth: 18,
          //   barGap: "50%",
          //   itemStyle: {
          //     normal: {
          //       color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
          //         { offset: 0, color: "rgba(228, 145, 72, 1)", },
          //         { offset: 1, color: "rgba(228, 145, 72, 0.3)", },
          //       ], false ),
          //     }
          //   },
          // },
        ]
      };
      chart.setOption(option);
      // 大屏适配
      window.addEventListener("resize", function () {
        chart.resize();
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.echartBar{
  width: 100%;
  height: 100%;
}
</style>